﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Common;
using CNCSoft.EData;

namespace CNCSoft.ErpApps.TMS.longGiaoVu
{
    public partial class frmHop : Form
    {
        SqlConnection conn = new SqlConnection(GlobalVariable.WSInfo.ConnectionString);
        DataSet ds = new DataSet();
        string _userid = GlobalVariable.WSInfo.UserID;

        public frmHop()
        {
            InitializeComponent();
        }

        private void frmHop_Load(object sender, EventArgs e)
        {
            LoadData();
            if (_userid.ToLower() == "tuptc")
            {
                btnThem.Enabled = true;
                btnLuu.Enabled = false;
                btnBoQua.Enabled = false;
            }
            else
            {
                btnThem.Enabled = false;
                btnLuu.Enabled = true;
                btnBoQua.Enabled = true;
            }
        }

        private void frmHop_Enter(object sender, EventArgs e)
        {
            if (_userid.ToLower() == "tuptc")
            {
                btnThem.Enabled = true;
                btnLuu.Enabled = false;
                btnBoQua.Enabled = false;
            }
            else
            {
                btnThem.Enabled = false;
                btnLuu.Enabled = true;
                btnBoQua.Enabled = true;
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            try
            {
                frmHopAdd frmAdd = new frmHopAdd();
                frmAdd.ShowDialog();
                LoadData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            UpdateData();
            LoadData();
        }

        private void btnBoQua_Click(object sender, EventArgs e)
        {
            ds.RejectChanges();
            LoadData();
        }

        #region "Coding"
        private void LoadData()
        {
            if (_userid.ToLower() == "tuptc")
            {
                LoadHop();
                gridControl_Data.DataSource = ds.Tables["TableHop"];
            }
            else
            {
                LoadHopByUser(_userid);
                gridControl_Data.DataSource = ds.Tables["TableHopByUser"];
            }
        }

        private void LoadHop()
        {
            try
            {
                if (ds.Tables["TableHop"] != null)
                    ds.Tables["TableHop"].Clear();
                string sql = @"usp_AllHop";
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                da.Fill(ds, "TableHop");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void LoadHopByUser(string userid)
        {
            try
            {
                if (ds.Tables["TableHopByUser"] != null)
                    ds.Tables["TableHopByUser"].Clear();
                string sql = @"usp_AllHopByUser @userid = '" + userid + "'";
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                da.Fill(ds, "TableHopByUser");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void UpdateData()
        {
            try
            {
                string sql = @"select * from Hop";
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                DataHelper.buildDataCommands(ref da);
                da.Update(ds, "TableHopByUser");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion
    }
}
